<?php

function getCategoryTree($cat_tree = array(),$pid = 0 , $level = 0){
	$sql = 'select * from category where pid='.$pid ;
	$query = mysql_query($sql);
	$result = array();
	while($row =mysql_fetch_assoc($query)){
		$result[] = $row;
	}
	$level_str = '';
	for($i = 0; $i < $level ; $i++){
		$level_str .= '|-';
	}
	foreach ($result as $value) {
		$pid = $value['id'];
		$value['name'] = '<font color="red">'.$level_str.'</font>'.$value['name'];
		$cat_tree[] = $value;
		$cat_tree = getCategoryTree($cat_tree,$pid,$level+1);
	}
	return $cat_tree;
}


function getCatTopTree(){
	$sql = 'select * from category where pid = 0';
	$query = mysql_query($sql);
	$tree = array();
	while($row = mysql_fetch_assoc($query)){
		$tree[] = $row;
	}
	return $tree;
}

/**
 * 删除无极分类
 * @param int $pid 当前分类id
 */
function detCategoryTree($pid = 0){
	$result = array();
	$sql = 'select * from category where pid = '.$pid;
	$query = mysql_query($sql);
	while($row = mysql_fetch_assoc($query)){
			$result[] = $row;
	}
	foreach($result as $r){
		$id = $r['id'];
		detCategoryTree($id);
	}
}
